1. Field of the Invention
This invention relates to a two-dimensional code reading apparatus for reading a matrix type two-dimensional code.
2. Related Art
Two-dimensional codes can record large amount of information compared with one-dimensional codes such as bar codes. In a two-dimensional code, each entry in a matrix is either black or white. That is, a two-dimensional code expresses data as a pattern of entries. These entries constituting basic units of data expression are called cells. To read data expressed by a two-dimensional code correctly, it is necessary to distinguish correctly the black/white type of each cell.
An example of this kind of two-dimensional code reading apparatus is disclosed in Japanese Patent Application Laid-Open No. 8-180125.
A two-dimensional code reading apparatus detects an image of a two-dimensional code. When processing this as image data, the two-dimensional code reading apparatus usually carries out processing in pixel units. Accordingly, the obtained image data of each cell is also made up of a plurality of pixels.
For example, suppose that an image G of a black cell has been read in as 3.times.3=9 pixels. Then suppose that of these nine pixels, due to print blurring or soiling of the two-dimensional code, three have been detected as white pixels WT. In determination of the type of the cell under such circumstances, the type of the majority of the pixels is determined to be the type of the cell. In the example shown in FIG. 14A, because there are six black pixels BK and three white pixels WT, the type of the cell is determined to be black.
The number of pixels for determining the type of each cell, that is, the reading scope for one cell, has normally been set on the basis of a predetermined reference pattern existing in the two-dimensional code. For example, position detection patterns A, B and C and an extension pattern D whose positions and sizes can be obtained at the initial stage of reading are provided in the two-dimensional code. The size of one cell (a height and a width) is obtained from one of these and the obtained size is used to determine a number of pixels as a reading scope for each cell. In consideration of for example reading position errors, this number of pixels is sometimes set so that a reading scope is slightly smaller than the cell size obtained.
However, the two-dimensional code is not always detected without distortion as shown in FIG. 14B. Sometimes the two-dimensional code is read at an angle and there is distortion in the detected image, as shown in FIG. 15. In this case, the cell size will change depending on which of the position detection patterns A, B and C and the extension pattern D is used to determine the cell size. In the example of FIG. 15, because the position detection patterns A and C become small as image data, the size of a reading scope of one cell determined on the basis of either of them is inevitably small and the number of pixels used for determining cell type then is small. The position detection pattern B, on the other hand, becomes larger as image data than the position detection patterns A and C. As a result, the number of pixels for determining cell type becomes large.
Consequently, it is difficult to make the determination of which cell size determined based on each of the position detection patterns and the extension pattern should be used to determine the size of the cells. For example, to determine the size of cells, it is conceivable to employ an average value of different cell sizes determined using each of the position detection patterns respectively; however, when this is done, type determination using an excessive number of pixels may be carried out on cells in the vicinity of the position detection patterns A and C and type determination using too few pixels is carried out on cells in the vicinity of the position detection pattern B and the extension pattern D.
In FIG. 16A, when the nine pixels (3.times.3 pixels) of the scope shown with a solid line is a suitable number of pixels for reading the cell and this number of pixels is correctly set, there are six black pixels and three white pixels in that scope. Accordingly the type of the cell is determined on a majority basis to be black. However, when the 25 pixels (5.times.5 pixels) of an excessively large scope is set as the number of pixels for determining the type of the cell, because there are eleven black pixels and fourteen white pixels, on the same majority basis the cell is determined to be white and the type of the cell thus is not determined correctly.
This is also the same in reverse. That is, a cell determined to be white with a suitable 25 pixels may be mistakenly determined to be black with an insufficient 9 pixels.
It is therefore a first object of the present invention to provide a two-dimensional code reading apparatus which can set a scope for determining cell types suitably even when there is distortion in the detected two-dimensional image.
Also, to carry out black/white determination of cells correctly, it is important to accurately determine the center position of each cell.
In reading processing for the kind of two-dimensional code shown in FIG. 14B, first, three position detection patterns A, B, C are detected from the characteristics of the pattern. Then, timing cell rows E, F wherein white and black cells are disposed alternately running between these position detection patterns A, B, C are detected. Then, by image processing, the center positions of the cells of the position detection patterns A, B, C and the timing cell rows E, F are detected. When the center positions of these cells have been determined, the center positions of the other cells are obtained by calculation using the coordinates of the center positions of these cells.
That is, when the position detection patterns are scanned, predetermined frequency component ratios can be obtained from them, and on this basis the center coordinates of the position detection patterns A, B, C are determined first. Also, because the overall shapes of the position detection patterns A, B, C are predetermined, the center positions of the cells making up the position detection patterns A, B, C can also be obtained by calculation. The positions of the timing cells rows E, F disposed in predetermined positions between the position detection patterns A, B, C can also be obtained by calculation. Since the timing cell rows E, F consist of black and white cells disposed alternating cell by cell, by finding the boundaries of the cells by image processing the center positions of the cells making up the timing cell rows E, F can also be determined.
Then, with the cells making up the position detection patterns A, B, C and the timing cell rows E, F whose positions have been determined as described above as reference cells, from coordinate values of these reference cells, it is possible to obtain by calculation on the basis of cell widths and heights the positions of the cells other than those of the position detection patterns A, B, C and the timing cell rows E, F, that is, the positions of the so-called data cells.
Because it is possible to obtain the positions of all of the data cells in this way, by reading the data cells from these positions it is possible to detect the data expressed by the two-dimensional code.
However, the two-dimensional code is not always detected without distortion as shown in FIG. 14B, and sometimes the two-dimensional code is read at an angle and there is distortion in the detected image, as shown in FIG. 15. In this kind of case, for data cells near the position detection patterns A, B, C or the timing cell rows E, F, the positional deviation between the reading positions obtained by calculation and the actual positions of the cells is small. Therefore, reading of the cells is carried out correctly. However, with progress away from the position detection patterns A, B, C and the timing cell rows E, F the positional deviation between the reading positions and the actual cell positions becomes larger and it becomes impossible to correctly determine the types of the cells.
It is therefore a second object of the invention to provide a two-dimensional code reading apparatus which can set positions for reading cells correctly even when there is distortion in the detected two-dimensional image.